<?php
/**
 * @see Alinks_ImportExport_Export_Abstract
 */
require_once('Alinks/ImportExport/Export/Abstract.php');

/**
 * Utility class for exporting the defined keyphrases in the
 * Microsoft Excel 2000 format
 *
 * Copyright (C) 2008  Sean Hickey
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: Excel2000.php 390 2008-07-03 05:40:51Z sean $
 * @package Alinks_ImportExport
 */
class Alinks_ImportExport_Export_Excel2000 extends Alinks_ImportExport_Export_Abstract
{
	/**
	 * Returns the keyphrases as an XML string
	 *
	 * Returns the keyphrases (and optionally the settings and factory
	 * settings) as an XML string.
	 *
	 * @return string
	 */
	public function export()
	{
		$doc = new DOMDocument('1.0', 'UTF-8');
		$root = $doc->appendChild(new DOMElement('html'));
		$head = $root->appendChild(new DOMElement('head'));
		$meta = $head->appendChild(new DOMElement('meta'));
		$meta->appendChild(new DOMAttr('http-equiv', 'Content-type'));
		$meta->appendChild(new DOMAttr('content', 'text/html;charset=utf-8'));
		$style = $head->appendChild(new DOMElement('style'));
		$style->appendChild(new DOMAttr('id', 'Classeur1_16681_Styles'));
		$body = $root->appendChild(new DOMElement('body'));
		$div = $body->appendChild(new DOMElement('div'));
		$div->appendChild(new DOMAttr('id', 'Classeur1_16681'));
		$div->appendChild(new DOMAttr('align', 'center x:publishsource="Excel"'));
		
		$table = $div->appendChild(new DOMElement('table'));
		$table->appendChild(new DOMAttr('x:str'));
		$table->appendChild(new DOMAttr('border', '0'));
		$table->appendChild(new DOMAttr('cellpadding', '0'));
		$table->appendChild(new DOMAttr('cellspacing', '0'));
		$table->appendChild(new DOMAttr('width', '100%'));
		$table->appendChild(new DOMAttr('style', 'border-collapse: collapse'));
		
		$this->_exportKeyphrases($table);
		
		$doc->formatOutput = true;
		return $this->_filter($doc->saveXML());
	}
	
	/**
	 * Returns the MIME type for the exported document
	 *
	 * @return string
	 */
	public function getMimeType()
	{
		return 'application/excel';
	}
	
	/**
	 * Returns the file extension for the exported document
	 *
	 * @return string
	 */
	public function getFileExt()
	{
		return 'xls';
	}
	
	/**
	 * Exports the keyphrases to the exported XML
	 *
	 * @param DOMElement $root The root XML element
	 */
	protected function _exportKeyphrases(DOMElement $root)
	{
		if (empty($this->_options[self::EXPORT_HASHES])) {
			$keyphrases = Alinks_Registry::get('Alinks_Keyphrase_Keeper')->fetchAll();
		} else {
			$keyphrases = Alinks_Registry::get('Alinks_Keyphrase_Keeper')->fetchByHashes($this->_options[self::EXPORT_HASHES]);
		}
		
		foreach($keyphrases as $keyphrase) {
			$tr = $root->appendChild(new DOMElement('tr'));
			
			$td = $tr->appendChild(new DOMElement('td', htmlspecialchars($keyphrase->getPhrase())));
			$td->appendChild(new DOMAttr('class', 'xl2216681'));
			$td->appendChild(new DOMAttr('nowrap'));
			
			$td = $tr->appendChild(new DOMElement('td', htmlspecialchars($keyphrase->getDescription())));
			$td->appendChild(new DOMAttr('class', 'xl2216681'));
			$td->appendChild(new DOMAttr('nowrap'));
			
			$td = $tr->appendChild(new DOMElement('td', date('Y-m-d H:i:s', $keyphrase->getCreated())));
			$td->appendChild(new DOMAttr('class', 'xl2216681')); 
			$td->appendChild(new DOMAttr('nowrap'));
			
			$td = $tr->appendChild(new DOMElement('td', $keyphrase->getFactory()));
			$td->appendChild(new DOMAttr('class', 'xl2216681')); 
			$td->appendChild(new DOMAttr('nowrap'));

			$td = $tr->appendChild(new DOMElement('td', htmlspecialchars($this->_getOptionsString($keyphrase->getOptions()))));
			$td->appendChild(new DOMAttr('class', 'xl2216681')); 
			$td->appendChild(new DOMAttr('nowrap'));
			
			$td = $tr->appendChild(new DOMElement('td', $keyphrase->getHash()));
			$td->appendChild(new DOMAttr('class', 'xl2216681'));
			$td->appendChild(new DOMAttr('nowrap'));
		}
	}
	
	/**
	 * Returns the keyphrase options as a string
	 *
	 * @param array $options The keyphrase options
	 * @return string
	 */
	protected function _getOptionsString(array $options)
	{
		$o = array();
		foreach($options as $key => $value) {
			$o[] = '"' . $key . '" = "' . $value . '"';
		}
		
		return join(', ', $o);
	}
	
	/**
	 * Some of the stuff output by DOM needs to be replaced
	 *
	 * @param string $string XML string
	 * @return string
	 */
	protected function _filter($string)
	{
		$string = str_replace('x:str=""', 'x:str', $string);
		$string = str_replace('nowrap=""', 'nowrap', $string);
		$string = str_replace('<style id="Classeur1_16681_Styles"/>', '<style id="Classeur1_16681_Styles"></style>', $string);
		$string = str_replace('<?xml version="1.0" encoding="UTF-8"?>',
		"<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">
		<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
		, $string);
		
		return $string;
	}
}